use HeThongSim
go
--Khách Hàng Đăng Ký
alter proc usp_DangKy 
	@email varchar(50) = null, 
	@matkhau varchar(50) = null, 
	@emailphu varchar(50) = null, 
	@tenkhachhang nvarchar(50) = null,
	@diachi nvarchar(50) = null,
	@dienthoai char(11)= null
as
	begin tran
		set tran isolation level Serializable
		--kiem tra dung dan' cua du~ lieu nhap vao
		if(@email is null or @matkhau is null or @emailphu is null or @tenkhachhang is null or @diachi is null or @dienthoai is null)
			begin
					rollback 
					return
			end

		--kiem tra email ton tai ?
		if(exists(select * from KhachHang with (updlock) where Email like @email))
			begin
					rollback
					return
			end
		waitfor delay '0:0:10'
		--them 1 dang ky
		begin try
			insert into KhachHang(Email, MatKhau, EmailPhu, TenKhachHang, DiaChi, DienThoai, TinhTrang) 
				values(@email, @matkhau, @emailphu, @tenkhachhang, @diachi, @dienthoai, N'Chờ')	
		end try
		begin catch
					declare @error nvarchar(1000)
					set @error = N'Lỗi: ' + error_message()
					raiserror (@error,16,1)
					rollback tran
					return
		end catch
	commit tran
go